//
//  MacauViewController.m
//  Bible Assistant
//
//  Created by Dyoung on 2017/7/23.
//  Copyright © 2017年 nixWork. All rights reserved.
//

#import "MacauViewController.h"
#import <SVProgressHUD.h>


#define DyColorFromRGB(rgbValue) [UIColor colorWithRed:((float)((rgbValue & 0xFF0000) >> 16))/255.0 green:((float)((rgbValue & 0xFF00) >> 8))/255.0 blue:((float)(rgbValue & 0xFF))/255.0 alpha:1.0]
#define DyScreenWidth [UIScreen  mainScreen].bounds.size.width
#define DyScreenHeight [UIScreen mainScreen].bounds.size.height
#define  DyStatusBarHeight 20
#define DyAppDelegate ((AppDelegate *)[UIApplication sharedApplication].delegate)

@interface MacauViewController ()<UIWebViewDelegate>

@end

@implementation MacauViewController
{
    UIWebView * webView;
    CGFloat homeViewWidth;
    CGFloat space;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
//    [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];
    
    webView = [[UIWebView alloc]initWithFrame:CGRectMake(0, DyStatusBarHeight, DyScreenWidth, DyScreenHeight-DyStatusBarHeight)];
    [self.view addSubview:webView];
    webView.backgroundColor = [UIColor blackColor];
    webView.delegate = self;
    if (self.isFit) {
         webView.scalesPageToFit = YES;
    }
   
    
    UIView * view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, DyScreenWidth, DyStatusBarHeight)];
    [self.view addSubview:view];
    view.backgroundColor = [UIColor blackColor];
    
    [self refreshAction];
    
    
    //移动的首页按钮：
    homeViewWidth = 35;
    space = 10;
    UIImageView * homeView = [[UIImageView alloc]initWithFrame:CGRectMake(DyScreenWidth-homeViewWidth-space, DyScreenWidth-homeViewWidth-50, homeViewWidth, homeViewWidth)];
    UITapGestureRecognizer * tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(homeViewTapAction:)];
    homeView.userInteractionEnabled = YES;
    [homeView addGestureRecognizer:tap];
    [self.view addSubview:homeView];
    homeView.image = [UIImage imageNamed:@"back"];
    
    UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(moveTest:)];
    [homeView addGestureRecognizer:pan];

    
}

-(UIStatusBarStyle)preferredStatusBarStyle
{
    return UIStatusBarStyleLightContent;
}

- (void)moveTest:(UIGestureRecognizer *)gestureRecognizer
{
    
    CGPoint locPoint = [gestureRecognizer locationInView:[[[UIApplication sharedApplication] delegate] window]];
    if (gestureRecognizer.state == UIGestureRecognizerStateEnded) {

        
        if (locPoint.x > DyScreenWidth/2) {
            locPoint.x = DyScreenWidth- homeViewWidth/2-space;
        }
        else{
            locPoint.x  = space+homeViewWidth/2;
        }
        [UIView animateWithDuration:0.2 animations:^{
           gestureRecognizer.view.center = locPoint;
        }];
       
    }
    else{
        gestureRecognizer.view.center = locPoint;
    }
    
    
}




-(void)refreshAction
{
    NSURL * url = [NSURL URLWithString:self.requestUrl];
    NSURLRequest * request = [NSURLRequest requestWithURL:url];
    dispatch_async(dispatch_get_main_queue(), ^{
        [webView loadRequest:request];
    });
    
    
}


- (void)homeViewTapAction:(UITapGestureRecognizer *)tap
{
    [webView goBack];
}



    
    
- (void)webViewDidStartLoad:(UIWebView *)webView{
    [SVProgressHUD show];
}
- (void)webViewDidFinishLoad:(UIWebView *)webView{
    [SVProgressHUD dismiss];
}


@end
